package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;
    @RequestMapping("/findAllRole")
    public Result findAllRole(){

        try {
            Set<Role> roles= menuService.findAllRole();
            return new Result(true,"查询所有角色成功!",roles);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询所有角色失败!");
        }
    }
    @RequestMapping("/findAllmenu")
    public Result findAllmenu(){
        try {
            List<Menu> list= menuService.findAllmenu();
            return new Result(true,"查询所有菜单成功!",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询所有菜单失败!");
        }
    }
//  菜单新增方法
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu,Integer[] checkitemIds,Integer[] checkitemIds1){
        //添加菜单时 加上层级  先判断层级 第一层不可以有父类 然后查询层级为1的数量 +1 作为该层级的path
        //如果是第二层级  查询父id相同的数量   +1  作为优先级的级别 然后拼接 父path+优先级
        try {
//            if (menu.getLevel()==1){
//                if (menu.getParentMenuId()!=null){
//                    return new Result(false,"第一层级,不可以有父菜单!");
//                }
//               int count= menuService.findBy1Count();
//                String path=count+1+"";
//                menu.setPath(path);
//                menuService.add(menu,checkitemIds,checkitemIds1);
//                return new Result(true,"新增菜单成功!");
//            }
//            //查询父id相同的数量 +1作为优先级的级别
//            int priority=menuService.findParent(menu.getParentMenuId());
//            menu.setPriority(priority+1);
//            //查询父path
//            String path=menuService.findParentPath(menu.getParentMenuId());
//            menu.setPath("/"+path+"-"+priority);
            menuService.add(menu,checkitemIds,checkitemIds1);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"新增菜单失败!");
        }
        return new Result(true,"新增菜单成功!");
    }
//   菜单分页查询方法
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
      return  menuService.findPage(queryPageBean.getQueryString(),queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
    }
//   根据菜单id查询关联角色方法
    @RequestMapping("/findRoleIdByMenuId")
    public Result findRoleIdByMenuId(int id){
        try {
            List<Integer> list= menuService.findRoleIdByMenuId(id);
            return new Result(true,"查询关联角色成功!",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询关联角色失败!");
        }
    }
    //   根据菜单id查询子菜单方法
    @RequestMapping("/findMenuIdByMenuId")
    public Result findMenuIdByMenuId(int id){
        try {
            List<Integer> list= menuService.findMenuIdByMenuId(id);
            return new Result(true,"查询子菜单成功!",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询子菜单失败!");
        }
    }
//  菜单编辑方法
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu,Integer[] checkitemIds,Integer[] checkitemIds1 ){
        try {
            menuService.edit(menu,checkitemIds,checkitemIds1);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"菜单编辑失败!");
        }
        return new Result(true,"菜单编辑成功!");
    }
//  菜单删除方法
    @RequestMapping("/delete")
    public Result delete(int id){
        try {
            menuService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"菜单删除失败!");
        }
        return new Result(true,"菜单删除成功!");
    }


    }

